package com.example.book.sys.menu.service;

import com.example.book.base.BaseService;
import com.example.book.common.shiro.ShiroUtils;
import com.example.book.domain.sys.SysUser;
import com.example.book.sys.menu.dao.SysMenuDao;
import com.example.book.domain.sys.SysMenu;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author haocj
 * 用户service
 */
@Service
public class SysMenuService extends BaseService<SysMenu, SysMenuDao> {

    @Resource
    private SysMenuDao sysMenuDao;

    /**
     * 查询菜单信息，上级菜单名称
     *
     * @param sysMenu
     * @return
     */
    @Override
    public SysMenu get(SysMenu sysMenu) {
        SysMenu sys = super.get(sysMenu);
        SysMenu menu = null;
        if (sys.getParentId() != null) {
            menu = super.get(sys.getParentId());
        }
        if (menu != null) {
            sys.setParentName(menu.getName());
        }
        return sys;
    }

    /**
     * 查询用户下菜单 超级管理查询所有
     * @return
     */
    public List<SysMenu> findUserMenu(){
        SysUser sysUser = ShiroUtils.getUser();
        if (sysUser.isSuperAdmin()){
            return sysMenuDao.findList(new SysMenu("0"));
        }
        return sysMenuDao.findUserMenu(sysUser.getId());
    }
}
